package cn.txm.org.controller;


import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.PageList;
import cn.txm.org.domain.Employee;
import cn.txm.org.domain.Shop;
import cn.txm.org.query.EmployeeQuery;
import cn.txm.org.query.ShopQuery;
import cn.txm.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "门店接口",description = "门店接口详细描述")
public class ShopController {
    @Autowired
    IShopService shopService;

    @GetMapping
    public List<Shop> loadAll(){
        return shopService.getAll();
    }

    // 修改保存 @PutMapping  有没有id
    @PutMapping
    //@GetMapping("/save")
    @ApiOperation(value = "添加或修改员工",notes = "如果有id就是修改，如果没有就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙！"+e.getMessage());
        }
    }

    // 删除   @DeleteMapping   "{id}"
    @DeleteMapping("{id}")
    public AjaxResult delById(@PathVariable("id") Long id){

        /*链式编程思维*/
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me()
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }

    /**
     * 分页查询店铺列表 + 支持高级查询
     * @return
     */
    @PostMapping
    public PageList<Shop> getPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            shopService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me()
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }

    @GetMapping("/{id}/{type}")
    public AjaxResult shopAudit(@PathVariable("id")Long id,@PathVariable("type")Integer type){
        try {
            shopService.shopAudit(id,type);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核失败"+ e.getMessage());
        }
    }

}
